﻿//#define RELEASE_BUILD

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if RELEASE_BUILD

// 发布的时候重载默认的debug，release正式包
public static class Debug
{   
    // 里面的内容在debug定义里面找log，logwarning，logerror，logexception，DrawLine
    // 另外，Object提示不明确引用（System与UnityEngine），查看debug定义，Exception为system，Object为UnityEngine
    public static void Log(object message, UnityEngine.Object context) { }
    public static void Log(object message) { }
    public static void LogError(object message, UnityEngine.Object context) { }
    public static void LogError(object message) { }
    public static void LogException(Exception exception, UnityEngine.Object context) { }
    public static void LogException(Exception exception) { }
    public static void DrawLine(Vector3 start, Vector3 end) { }
    public static void DrawLine(Vector3 start, Vector3 end, Color color) { }
}

#endif

public static class Debugger
{
    public static void Log(object message)
    {
        #if RELEASE_BUILD
        #else
            if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {
                DebugMgr.Log(message.ToString());
            }
            else
            {
                Debug.Log(message.ToString());
            }
        #endif
    }

    public static void LogWarning(object message)
    {
#if RELEASE_BUILD
#else
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            DebugMgr.Log(message.ToString());
        }
        else
        {
            Debug.Log(message.ToString());
        }
#endif
    }


    public static void LogError(object message)
    {
#if RELEASE_BUILD
#else
        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
        {
            DebugMgr.Log(message.ToString());
        }
        else
        {
            Debug.Log(message.ToString());
        }
#endif
    }
}



/// <summary>
/// 调试管理器
/// </summary>
public class DebugMgr : MonoBehaviour {

    public static bool IsDebug = false;
    public static int DebugCount = 0;

    //debug信息列表
    public static List<string> debugInfoList = new List<string>();

    /// <summary>
    /// debug开关,暂时没用
    /// </summary>
    public static void Switch()
    {
        IsDebug = !IsDebug;
        if (IsDebug)
        {
            ++DebugCount;
            debugInfoList.Clear();
            debugInfoList.Add(DebugCount.ToString());//调试信息条数
        }
    }

    public static void Log(string str)
    {
        debugInfoList.Add(DateTime.Now + ":[" + str + "]");//打印时间+信息，方便与服务器对比该时间的消息
    }

    public Rect debugInfoWindowRect = new Rect(80, 20, 800, 2000);

    private void OnGUI()
    {
        GUILayout.Space(40);
        if (GUILayout.Button("ShowDebug"))
        {
            IsDebug = !IsDebug;
        }
        else if (GUILayout.Button("ClearInfo"))
        {
            debugInfoList.Clear();
        }

        if (IsDebug)
        {
            debugInfoWindowRect = GUILayout.Window(1, debugInfoWindowRect, DebugWindow, "Debug Info Window");
        }
    }

    private Vector2 debugInfoPos = new Vector2(0, 0);
    void DebugWindow(int winID)
    {
        debugInfoPos = GUILayout.BeginScrollView(debugInfoPos, false, true, GUILayout.Width(800), GUILayout.Height(600));
        GUILayout.Space(30);
        GUILayout.BeginVertical();

        foreach (string str in debugInfoList)
        {
            GUILayout.Label(str, GUILayout.Width(800));
        }

        GUILayout.EndVertical();
        GUILayout.EndScrollView();
    }
}
